package com.hope4cc.dao.Impl;

import com.hope4cc.dao.AccountDao;
import com.hope4cc.entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * 文件名：AccountDaoImpl
 * 创建者：hope
 * 邮箱：1602774287@qq.com
 * 微信：hope4cc
 * 创建时间：2022/10/6-16:18
 * 描述：
 */
public class AccountDaoImpl implements AccountDao {

     private QueryRunner queryRunner;

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    @Override
    public int addAccount(Account account) throws SQLException {
        String sql="insert into account value(null,?,?)";
        return queryRunner.update(sql,account.getName(),account.getMoney());
    }

    @Override
    public int deleteAccount(Integer id) throws SQLException {
        String sql="delete from account where id=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public int updateAccount(Account account) throws SQLException {
        String sql="update  account set name=?,money=? where id=?";
        return queryRunner.update(sql,account.getName(),account.getMoney(),account.getId());
    }

    @Override
    public Account selectAccountById(Integer id) throws SQLException {
        String sql="select * from account where id=?";
        return queryRunner.query(sql,new BeanHandler<>(Account.class),id);
    }
}
